﻿package com.ui.controls
{
	import com.ui.core.PhaseState;
	import com.ui.core.ScaleMode;
	import com.ui.core.UIComponent;
	import com.ui.data.ButtonData;
	import com.ui.layout.GLayout;
	import com.ui.manager.UIManager;
	import com.utils.MusicUtil;

	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;

	/**
	 * Game Button
	 * 
	 * @author Cafe
	 * @version 20100708
	 */
	public class Button extends UIComponent
	{
		protected var _data:ButtonData;
		// 正常
		protected var _upSkin:DisplayObject;
		// 鼠标在上面
		protected var _overSkin:DisplayObject;
		// 鼠标按下
		protected var _downSkin:DisplayObject;
		// 禁用
		protected var _disabledSkin:DisplayObject;
		protected var _label:Label;
		protected var _current:DisplayObject;
		protected var _phase:int = PhaseState.UP;

		override protected function create():void
		{
			_upSkin = UIManager.getUI( _data.upAsset );
			_overSkin = UIManager.getUI( _data.overAsset );
			_disabledSkin = _downSkin = UIManager.getUI( _data.downAsset );
			// _disabledSkin = UIManager.getUI( _data.disabledAsset );
			_current = _upSkin;
			addChild( _current );
			switch(_data.scaleMode)
			{
				case ScaleMode.SCALE_WIDTH:
					_height = _upSkin.height;
					break;
				case ScaleMode.SCALE_NONE:
					_width = _upSkin.width;
					_height = _upSkin.height;
					break;
			}
			if (_data.labelData.text != "")
			{
				initLabel();
			}
		}

		protected function initLabel():void
		{
			if (_label == null)
			{
				_label = new Label( _data.labelData );
				addChild( _label );
				GLayout.layout( _label );
			}
		}

		override protected function layout():void
		{
			_upSkin.width = _width;
			_upSkin.height = _height;
			if (_overSkin != null)
			{
				_overSkin.width = _width;
				_overSkin.height = _height;
			}
			if (_downSkin != null)
			{
				_downSkin.width = _width;
				_downSkin.height = _height;
			}
			if (_disabledSkin != null)
			{
				_disabledSkin.width = _width;
				_disabledSkin.height = _height;
			}
		}

		override protected function onEnabled():void
		{
			if (_label)
				_label.enabled = _enabled;
			viewSkin();
		}

		private function addEvents():void
		{
			addEventListener( MouseEvent.ROLL_OVER, rollOverHandler );
			addEventListener( MouseEvent.ROLL_OUT, rollOutHandler );
			addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
		}

		private function removeEvents():void
		{
			removeEventListener( MouseEvent.ROLL_OVER, rollOverHandler );
			removeEventListener( MouseEvent.ROLL_OUT, rollOutHandler );
			removeEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			removeEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
		}

		// 表随便用- - by sjb
		public function disableWithTooltip():void
		{
			_enabled = false;
			viewSkin();
		}

		override protected  function onShow():void
		{
			super.onShow();
			addEvents();
		}

		override protected function onHide():void
		{
			super.onHide();
			removeEvents();
			if (!_enabled) return;
			_phase = PhaseState.UP;
			viewSkin();
		}

		protected function rollOverHandler( event:MouseEvent ):void
		{
			if (!_enabled) return;
			_phase = PhaseState.OVER;
			viewSkin();
		}

		protected function rollOutHandler( event:MouseEvent ):void
		{
			if (!_enabled) return;
			_phase = PhaseState.UP;
			viewSkin();
		}

		protected function mouseDownHandler( event:MouseEvent ):void
		{
			if (MusicUtil.isSoundLoaded)
			{
				var soundClass:Class = getDefinitionByName( "eff_0" ) as Class;
				MusicUtil.playSfx( soundClass );
			}
			if (!_enabled) return;
			_phase = PhaseState.DOWN;
			viewSkin();
			stage.focus = null;
		}

		protected function mouseUpHandler( event:MouseEvent ):void
		{
			if (!_enabled) return;
			_phase = ((event.currentTarget == this) ? PhaseState.OVER : PhaseState.UP);
			viewSkin();
		}

		protected function viewSkin():void
		{
			var textColor:Number = -1;
			if (!_enabled)
			{
				textColor = _data.disabledColor;
				_current = swap( _current, _disabledSkin );
			}
			else
			{
				if (label&&label.textField.textColor == _data.disabledColor)
				{
					textColor = _data.upColor;
				}
				if (_phase == PhaseState.UP)
				{
					if (_data.changeColor)
						textColor = _data.upColor;
					_current = swap( _current, _upSkin );
				}
				else if (_phase == PhaseState.OVER)
				{
					if (_data.changeColor)
						textColor = _data.rollOverColor;
					_current = swap( _current, _overSkin );
				}
				else if (_phase == PhaseState.DOWN)
				{
					if (_data.changeColor)
						textColor = _data.downColor;
					_current = swap( _current, _downSkin );
				}
			}
			if (textColor != -1 && label)
				label.textColor = textColor;
		}

		public function Button( data:ButtonData )
		{
			_data = data;
			super( data );
		}

		public function resetColor( textColor:uint, rollOverColor:uint, upColor:uint, downColor:uint ):void
		{
			_data.labelData.textColor = textColor;
			_data.rollOverColor = rollOverColor;
			if (!_enabled)
			{
				if (_data.changeColor)
					_label.textColor = _data.disabledColor;
			}
			else if (_phase == PhaseState.UP)
			{
				if (_data.changeColor)
					_label.textColor = upColor;
			}
			else if (_phase == PhaseState.OVER)
			{
				if (_data.changeColor)
					_label.textColor = rollOverColor;
			}
			else if (_phase == PhaseState.DOWN)
			{
				if (_data.changeColor)
					_label.textColor = downColor;
			}
		}

		public function set text( value:String ):void
		{
			initLabel();
			_label.text = value;
			GLayout.layout( _label );
		}

		public function set htmlText( value:String ):void
		{
			initLabel();
			_label.htmlText = value;
			GLayout.layout( _label );
		}

		public function get label():Label
		{
			return _label;
		}

		public function set icon( value:BitmapData ):void
		{
			_label.icon.bitmapData = value;
			GLayout.layout( _label );
		}

		public function set iconGray( value:Boolean ):void
		{
			_label.icon.gray = value;
		}

		public function set upSkin( upSkin:DisplayObject ):void
		{
			_upSkin = upSkin;
			viewSkin();
		}

		public function set overSkin( overSkin:DisplayObject ):void
		{
			_overSkin = overSkin;
			viewSkin();
		}

		public function set downSkin( downSkin:DisplayObject ):void
		{
			_downSkin = downSkin;
			viewSkin();
		}

		public function set disabledSkin( disabledSkin:DisplayObject ):void
		{
			_disabledSkin = disabledSkin;
			viewSkin();
		}
	}
}
